<template>
  <MainLayout>
    <div class="tag-list">
      <h1 class="page-title">标签列表</h1>
      <div class="tag-cloud" v-if="tags.length">
        <router-link
          v-for="tag in tags"
          :key="tag.id"
          :to="`/tags/${tag.slug}`"
          class="tag-item"
          :style="getTagStyle(tag.document_count)"
        >
          {{ tag.name }}
          <span class="tag-count">({{ tag.document_count }})</span>
        </router-link>
      </div>
      <div v-else class="empty-state">暂无标签</div>
    </div>
  </MainLayout>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import MainLayout from '../components/layout/MainLayout.vue'
import { getTagList } from '../api/tag'

const tags = ref([])

// 获取标签列表
const fetchTags = async () => {
  try {
    const res = await getTagList()
    console.log('原始标签数据:', res)
    
    // 检查响应结构
    if (res?.code === 0 && Array.isArray(res?.data)) {
      // 过滤掉无效的标签（没有slug或name为###的标签）
      tags.value = res.data.filter(tag => 
        tag.slug && 
        tag.slug.trim() !== '' && 
        tag.name !== '###'
      )
      console.log('处理后的标签数据:', tags.value)
    } else {
      console.error('标签数据格式不正确:', res)
    }
  } catch (error) {
    console.error('获取标签列表失败:', error)
  }
}

// 根据文档数量计算标签样式
const getTagStyle = (count = 0) => {  // 添加默认值
  // 设置最小和最大字体大小
  const minSize = 14
  const maxSize = 28
  
  // 根据文档数量计算字体大小
  const size = Math.max(minSize, Math.min(maxSize, minSize + Math.log2(count + 1) * 2))
  
  return {
    fontSize: `${size}px`,
    opacity: 0.6 + Math.min(0.4, count / 20) // 根据数量调整透明度
  }
}

onMounted(() => {
  fetchTags()
})
</script>

<style scoped>
.tag-list {
  padding: 20px;
}

.page-title {
  font-size: 32px;
  font-weight: bold;
  margin-bottom: 40px;
  color: #333;
}

.tag-cloud {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  padding: 20px;
  justify-content: center;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.tag-item {
  text-decoration: none;
  color: #409EFF;
  padding: 8px 16px;
  border-radius: 20px;
  background: #ecf5ff;
  transition: all 0.3s ease;
  display: inline-flex;
  align-items: center;
}

.tag-item:hover {
  transform: translateY(-2px);
  background: #409EFF;
  color: white;
}

.tag-count {
  margin-left: 4px;
  font-size: 0.8em;
  opacity: 0.8;
}

.empty-state {
  text-align: center;
  padding: 40px;
  color: #999;
  font-size: 14px;
  background-color: #f5f5f5;
  border-radius: 8px;
}
</style> 